from flask import Blueprint,request,jsonify
import json
from db import db1
from utils.jwtc import myjwt
from utils.acl import acl
from utils.redisc import red
bp_user = Blueprint('user',__name__)

@bp_user.route('/login',methods=['POST'])
def login():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    sql = "select * from user where username='%s' and password='%s'"%(data['username'],data['password'])
    res = db1.find(sql)
    data = {'id':res['id'],'username':res['username']}
    token = myjwt.jwt_encode(data)
    pagelist = acl.get_interface_reslist(res['id'])
    interfacelist = acl.get_interface_reslist(res['id'])
    key = 'interface'+str(res['id'])
    red.insert_set(key,interfacelist)
    # print(res.find_set(key))
    if res:
        return jsonify({'code':200,'userid':res['id'],'username':res['username'],'token':token,'pagelist':pagelist})
    else:
        return jsonify({'code':20010,'mes':'用户名或密码错误'})